<?php
/**
 * @file
 * Displays the choices and forms to add a deed.
 *
 * Usage:
 *  A page that wants to display the choices and forms to add a deed should
 *  include/require this page (once).
 *
 * Parameter(s):
 *  - None.
 *
 * Global variable(s) set:
 *  - None.
 *
 * Global variable(s) unset:
 *  - None.
 *
 * Exception(s):
 *  - None.
 *
 * This file includes/requires:
 *  - h_greenindeed.php
 *  - h_deed.php
 *  - h_jqui.php
 *  - db_mysql_greenindeed.php
 *  - db_mysql_connect.php
 *  - db_mysql_disconnect.php
 *  - exception_msg.php
 */

// header file(s) ==============================================================
require_once "h_greenindeed.php";
require_once "h_deed.php";
require_once $PATH_COMMON . "php/h_jqui.php";


// constant(s) =================================================================
//! HTML of input field (start).
define(
  "DEED_INPUT_FIELD_START",
  "<input type='text' name='" . NAME_DEED_ADD_INPUT . "' value='"
);
//! HTML of input field (end).
define("DEED_INPUT_FIELD_END", "'>");
//! HTML of input field (start & end).
$DEED_INPUT_FIELDS = array(DEED_INPUT_FIELD_START, DEED_INPUT_FIELD_END);

//! Delimiter in deed statement (start).
define("DELIMITER_DEED_STATEMENT_START", "<%");
//! Delimiter in deed statement (end).
define("DELIMITER_DEED_STATEMENT_END", "%>");
//! Delimiter in deed statement (start & end).
$DELIMITERS_DEED_STATEMENT = array(
  DELIMITER_DEED_STATEMENT_START, DELIMITER_DEED_STATEMENT_END
);


// implementation ==============================================================
// message div
//! ID of containing DIV for error message.
$ID_ERR_DIV = ID_ADD_ERR_DIV;
//! ID of SPAN for error message.
$ID_ERR_MSG = ID_ADD_ERR_MSG;
//! ID of containing DIV for info message.
$ID_INFO_DIV = ID_ADD_INFO_DIV;
//! ID of SPAN for info message.
$ID_INFO_MSG = ID_ADD_INFO_MSG;
?>
<?php require "div_msg.php" ?>
<?php
try {
  // connect to database
  require "db_mysql_greenindeed.php";
  require $PATH_COMMON . "php/db_mysql_connect.php";

  // get deed types
  $deed_types = mysql_query(
    "select     *
       from     TYPES_DEED
       where    LOCALE = '" . $locale_user . "'
       order by DEED_TYPE_ID"
  );
  $sql_error  = mysql_error();

  // disconnect from database
  require $PATH_COMMON . "php/db_mysql_disconnect.php";

  // check result
  if (false == $deed_types) {
    // error executing query
    throw new Exception($sql_error);
  }

  // tabs
?>
<div id="<?php echo ID_DIV_TABS ?>">
  <ul>
    <?php
  // create a tab for each deed type
  $deed_inputs     = array();
  $deed_examples   = array();
  $deed_searches   = array();
  $deed_statements = array();
  while ($deed_type = mysql_fetch_array($deed_types)) {
    // retreive deed type id
    $deed_type_id = $deed_type["DEED_TYPE_ID"];
    // retreive deed statement placeholders
    $deed_statement_search = explode(
      DELIMITER_DEED_STATEMENT, $deed_type["DEED_STATEMENT_SEARCH"]
    );
    $deed_example = explode(
      DELIMITER_DEED_STATEMENT, $deed_type["DEED_EXAMPLE"]
    );
    // add name (always first element)
    $deed_statement = str_replace(
      array_shift($deed_statement_search),
      $deed_example[0],
      $deed_type["DEED_STATEMENT"]
    );
    // add input fields
    $deed_inputs[$deed_type_id] = str_replace(
      $DELIMITERS_DEED_STATEMENT, $DEED_INPUT_FIELDS, $deed_statement
    );
    // parse example
    $deed_examples[$deed_type_id] = str_replace(
      explode(DELIMITER_DEED_STATEMENT, $deed_type["DEED_STATEMENT_SEARCH"]),
      $deed_example,
      $deed_type["DEED_STATEMENT"]
    );
    // save deed statement placeholders (for publishing to wall)
    $deed_searches[$deed_type_id] = implode(
      DELIMITER_DEED_STATEMENT, $deed_statement_search
    );
    // save deed statement (for publishing to wall)
    $deed_statements[$deed_type_id] = $deed_statement;
    // create tab
    ?>
    <li><a href="#<?php echo ID_DIV_TABS ?>-<?php echo $deed_type_id ?>">
      <?php echo $deed_type["DEED_TYPE"] ?>
    </a></li>
    <?php
  }
    ?>
  </ul>
  <?php // tab contents ?>
  <?php foreach ($deed_inputs as $deed_type_id => $deed_input) { ?>
  <div id="<?php echo ID_DIV_TABS ?>-<?php echo $deed_type_id ?>"
       style="<?php echo STYLE_FONTSIZE_TAB_CONTENT ?>"
  >
    <form name="<?php echo NAME_DEED_ADD_FORM_PREFIX . $deed_type_id ?>">
    <table width="90%">
      <tr>
        <td nowrap>
          <?php // deed input fields ?>
          <?php echo $deed_input ?>
          <?php // 'add' button ?>
          <a href="#"
             class="<?php echo CSS_CLASS_BUTTON_ADD    . " "
                               . JQUI_CSS_GROUP_BUTTON . " "
                               . JQUI_CSS_CLASS_BUTTON_LEFT
                    ?>"
             id="<?php echo $deed_type_id ?>"
          >
            <span class="ui-icon ui-icon-plusthick"></span>
            <?php echo LBL_DEED_ADD ?>
          </a>
        </td>
      </tr>
      <tr>
        <td>
          <?php // example deed ?>
          <?php echo LBL_DEED_EXAMPLE . $deed_examples[$deed_type_id] ?>
        </td>
      </tr>
      <tr>
        <td>
          <?php // deed publish checkbox ?>
          <input type="checkbox"
                 name="<?php echo NAME_DEED_ADD_PUBLISH ?>"
                 checked
          ><?php echo LBL_DEED_PUBLISH ?>
        </td>
      </tr>
    </table>
    <?php // deed statement info (for publishing to wall) ?>
    <input type="hidden"
           name="<?php echo NAME_DEED_ADD_STATEMENT_SEARCH ?>"
           value="<?php echo $deed_searches[$deed_type_id] ?>"
    >
    <input type="hidden"
           name="<?php echo NAME_DEED_ADD_STATEMENT ?>"
           value="<?php echo $deed_statements[$deed_type_id] ?>"
    >
    </form>
  </div>
  <?php } ?>
</div>
<?php
} catch (Exception $ex) {
  // show exception message
  require $PATH_COMMON . "php/exception_msg.php";
}
?>
